# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=mosquitto
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.14
pkgrel=1
pkgdesc="An Open Source MQTT Broker (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://mosquitto.org"
license=('EPL' 'EDL')
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-cjson"
         "${MINGW_PACKAGE_PREFIX}-libwebsockets"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-winpthreads"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' '!strip' '!buildflags')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/eclipse/mosquitto/archive/v${pkgver}.tar.gz"
        '001-mosquitto-2.0.13-win-threads.patch'
        '004-mosquitto-2.0.13-make-man.patch')
sha256sums=('c0ce97b1911d1769ccfd65da237e919fd7eaa60209fd190c113d63fbd0c40347'
            'cb7117abf7ea9ec7c5895277cb0de0e04fba7d7a5ff341ae926565bfb95bcc21'
            '8f2a4e8ba68a225fba339aa2550ba1de697a469dc248497c9592dfa8e6ff3bec')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/001-mosquitto-2.0.13-win-threads.patch"
  patch -p1 -i "${srcdir}/004-mosquitto-2.0.13-make-man.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_SBINDIR=bin \
    -DWITH_STATIC_LIBRARIES=ON \
    -DWITH_PIC=ON \
    ../${_realname}-${pkgver}

  cmake --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ctest ./ || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR=${pkgdir} cmake --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
